package br.com.werp.regras.geral;

import java.io.PrintWriter;


/**
 * @author Sobreira
 * Criado em 11/08/2004
 *
 */
public class ErroPadrao extends Exception {
    private static final long serialVersionUID = 2978756408546890825L;
    private String message = null;
    private Exception exception;
    private long numErro;

    public ErroPadrao(Exception e, long num) {
        this(e);
        numErro = num;
    }

    public ErroPadrao(Exception e) {
        if (e == null) {
            ErroPadrao erroPadrao = new ErroPadrao("NullPointerException");
            exception = erroPadrao;
            numErro = erroPadrao.getNumErro();
        } else {
            exception = e;
            message = e.getMessage();

            if ("null".equals(message) || (message == null)) {
                message = "NullPointerException em :<br>";

                if (e instanceof ErroPadrao) {
                    message += ((ErroPadrao) e).getException().getStackTrace()[0].toString();
                } else {
                    message += e.getStackTrace()[0].toString();
                }
            }

            if (exception instanceof ErroPadrao) {
                ErroPadrao erroPadrao = (ErroPadrao) exception;
                numErro = erroPadrao.getNumErro();
            } else {
                numErro = System.currentTimeMillis();
            }
        }
    }

    public ErroPadrao() {
        exception = this;
        numErro = System.currentTimeMillis();
    }

    public ErroPadrao(String string) {
        this.message = string;
        numErro = System.currentTimeMillis();
        exception = this;
    }

    public Exception getException() {
        return exception;
    }

    public String getClasseErroCriada() {
        if (exception != null) {
            return exception.getClass().getName();
        }

        return this.getClass().getName();
    }

    public StackTraceElement[] getStackTraceResumido() {
        if (exception == null) {
            exception = new ErroPadrao("NullPointerException");
        }

        StackTraceElement[] stackTraceElements = exception.getStackTrace();
        int qdtde = 10;

        if (stackTraceElements.length < 10) {
            qdtde = stackTraceElements.length;
        }

        StackTraceElement[] stackTraceElementsReturn =
            new StackTraceElement[qdtde];
        System.arraycopy(stackTraceElements, 0, stackTraceElementsReturn, 0,
            qdtde);

        return stackTraceElementsReturn;
    }

    public void printStackTraceResumido(PrintWriter s) {
        StackTraceElement[] trace = getStackTraceResumido();
        s.println(getMessage());

        for (int i = 0; i < trace.length; i++)
            s.println("\tat " + trace[i]);

        s.flush();
    }

    public long getNumErro() {
        return numErro;
    }

    public void setNumErro(long numErro) {
        this.numErro = numErro;
    }

    public String getLocalizedMessage() {
        return super.getLocalizedMessage();
    }

    public String toString() {
        if (message != null) {
            return message.toString();
        } else if (exception != null) {
            return exception.getMessage();
        } else {
            return "NullPointerException";
        }
    }

    public String getMessage() {
        return message;
    }

    public String htmlBlink(String string) {
        return "<BLINK>" + string + "</BLINK>";
    }

    public void printStackTrace() {
        if (exception == null) {
            exception = new ErroPadrao("NullPointerException");
        }

        if (exception instanceof ErroPadrao) {
            printStackTraceResumido(new PrintWriter(System.out));
        } else {
            exception.printStackTrace();
        }
    }
}
